/*

HOW TO USE

var alert:UIAlertView = new UIAlertView( design_mc );
alert.title = "確認";
alert.message = "実行してもよろしいですか？";
alert.addButtonWithTitle( "いいえ" );
alert.addButtonWithTitle( "はい" );
alert.x = 100;
alert.y = 200;
alert.show( stage );

*/

package cocoaas3{

	import jp.progression.casts.*;
	import jp.progression.commands.display.*;
	import jp.progression.commands.lists.*;
	import jp.progression.commands.managers.*;
	import jp.progression.commands.media.*;
	import jp.progression.commands.net.*;
	import jp.progression.commands.tweens.*;
	import jp.progression.commands.*;
	import jp.progression.data.*;
	import jp.progression.events.*;
	import jp.progression.scenes.*;
	import jp.nium.core.debug.Logger;


	import flash.display.*;
	import flash.geom.*;
	import flash.events.*;
	import caurina.transitions.*;
	import caurina.transitions.properties.*;

	import jp.dividual.nativeExtensions.*;

	public class UIAlertView extends UIView {

		// original
		public var title:String;
		public var message:String;
		public var cancelButtonIndex:int;
		public function get numberOfButtons():uint{
			return _buttons_array.length;
		}

		// custom
		public var view:MovieClip;
		private var _buttons_array:Array = new Array();
		private var _container:Sprite;

		public function UIAlertView( $view:MovieClip ){
			DisplayShortcuts.init();
			ColorShortcuts.init();

			view = $view;
			addChild( view );

			view.title_txt.autoSize = "center";
			view.message_txt.autoSize = "center";
			view.button0.label_txt.autoSize = "left";
			view.button1.label_txt.autoSize = "left";

			view.button0.buttonMode = true;
			view.button0.mouseChildren = false;
			view.button0.addEventListener( MouseEvent.CLICK, _onClickButton );
			view.button1.buttonMode = true;
			view.button1.mouseChildren = false;
			view.button1.addEventListener( MouseEvent.CLICK, _onClickButton );
		}


		private function _onClickButton( e:MouseEvent ):void{
			var index:uint;
			switch( e.target ){
				case view.button0:
					index = 0;
					break;
				case view.button1:
					index = 1;
					break;
			}
			var ne:UIAlertViewEvent = new UIAlertViewEvent( UIAlertViewEvent.CLICKED_BUTTON, index );
			self.dispatchEvent( ne );

			_close();
		}


		public function addButtonWithTitle( title:String ):void{
			_buttons_array.push( title );
		}

		public function show( container:Sprite ):void{
			_container = container;

			view.title_txt.text = this.title;
			view.message_txt.text = this.message;

			// ボタン配置
			switch( numberOfButtons ){
				case 0:
					view.button0.visible = false;
					view.button1.visible = false;
					view.button0.x = view.button0.y = 0;
					view.button1.x = view.button1.y = 0;
					break;
				case 1:
					view.button1.visible = false;
					view.button0.bg_mc.width = 254;
					view.button0.label_txt.text = _buttons_array[0];
					view.button0.label_txt.x = view.button0.bg_mc.width/2 - view.button0.label_txt.width/2;
					break;
				case 2:
					view.button0.label_txt.text = _buttons_array[0];
					view.button0.label_txt.x = view.button0.bg_mc.width/2 - view.button0.label_txt.width/2;
					view.button1.label_txt.text = _buttons_array[1];
					view.button1.label_txt.x = view.button1.bg_mc.width/2 - view.button1.label_txt.width/2;
					break;
			}

			// レイアウト調整
			if( numberOfButtons == 0 ){
				view.bg_mc.height = view.message_txt.y + view.message_txt.height + 20;
			} else {
				view.button0.y = view.message_txt.y + view.message_txt.height + 20;
				view.button1.y = view.button0.y;
				view.bg_mc.height = view.button0.y + view.button0.height + 10;
			}

			// センタリング
			view.x = 0 - view.width/2;
			view.y = 0 - view.height/2;

			_container.mouseChildren = false;
			_container.mouseEnabled = false;
			Tweener.addTween( _container, {"_brightness":-1, time:0.5} );
			self.scaleX = self.scaleY = 0;

			var plist:ParallelList = new ParallelList();
			plist.addCommand(
				new AddChild( _container.stage, self ),
				new DoTweener( self, {"_scale":1, transition:"easeOutBack", time:0.3} ),
			null);
			plist.execute();
		}


		private function _close():void{
			Tweener.addTween( _container, {"_brightness":0, time:0.5} );
			_container.mouseChildren = true;
			_container.mouseEnabled = true;

			var slist = new SerialList();
			slist.addCommand(
				new DoTweener( self, {"_autoAlpha":0, "_scale":0.9, time:0.3} ),
				new RemoveChild( _container.stage, self ),
			null);
			slist.execute();
		}

	}
}





















